SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE  PROCEDURE [dbo].[SP_TWSACA_REPO_Q90]    
/*--------------------------------------------------------*/    
/*---Empresa              : OFISIS S.A.     --*/    
/*---Cliente              : OFISIS      --*/    
/*---Sistema              : CALCULOS SALDOS Caja  --*/    
/*---M+dulo               : Empresa      --*/    
/*---Programa             : CALCULA SALDO INICIAL  --*/    
/*---Script               : TMSABA_M.SQL    --*/    
/*---Nombre SP            : SP_TWSACA_REPO_Q90   --*/    
/*---Desarrollado por     : DCHIANG      --*/    
/*---Fecha Creacion    : 21/02/2005     --*/     
/*---Base Datos           : SQLSERVER     --*/    
/*---Versi+n              : 8.0                         --*/    
/*---Invoca a SP          :                             --*/    
/*--------------------------------------------------------*/    
/*--------------------------------------------------------*/    
/*---Modificado 1 por     : DCHIANG      --*/    
/*---Fecha Modificaci+n   : 21/02/2005     --*/     
/*---Detalle Modificaci+n : A SQL      --*/    
/*--------------------------------------------------------*/      
/*--------------------------------------------------------*/    
/*---Comentarios          --*/    
/*---             --*/    
/*---             --*/    
/*--------------------------------------------------------*/      
/*--------------------------------------------------------*/    
/* Drop Proc SP_TWSACA_REPO_Q90       --*/    
/* Grant execute On SP_TWSACA_REPO_Q90 To Public  --*/    
/* SP_TWSACA_REPO_Q90 '01','MBEHAR','','','01/01/2008' --*/    
/*--------------------------------------------------------*/    
@ISCO_EMPR TD_VC_002,     
@ISCO_USUA TD_VC_008,     
@ISCA_WH01 VARCHAR(125),     
@ISCA_WH02 VARCHAR(130),     
@ISFE_OPER VARCHAR(10)    
 As    
Create Table #TWPOSI_CAJA_Q01_01    
( CO_EMPR varchar(02) NULL,    
CO_CAJA varchar(08) NULL,    
IM_INGR Numeric(16,4) null,    
IM_EGRE Numeric(16,4) null,    
IM_CHEQ_CART Numeric(16,4) null )    
    
Create Table #TWPOSI_CAJA_Q01_02    
( CO_EMPR varchar(02) NULL,    
CO_CAJA varchar(08) NULL,    
IM_INGR Numeric(16,4) null,    
IM_EGRE Numeric(16,4) null,    
IM_CHEQ_CART Numeric(16,4) null )    
    
    
Create Table #TWSACA_REPO    
(CO_EMPR CHAR(02) NULL,     
 CO_CAJA VARCHAR(08) NULL,    
 AA_CAJA VARCHAR(04) NULL,    
 MM_CAJA VARCHAR(02) NULL,    
 IM_SALD_INIC Numeric(16,4) NULL)     
    
Create Table #TWSACA_REPO_AUXI(     
 CO_EMPR VARCHAR(02) NULL,    
 CO_CAJA VARCHAR(08) NULL,    
 IM_SALD_INIC Numeric(16,4) NULL)     
    
-- DECLARO VARIABLES DE TRABAJO    
    
DECLARE    
@VSCO_USUA TD_VC_008,    
@VSCA_WHER VARCHAR(255),    
@VSCA_WH01 VARCHAR(125),    
@VSCA_WH02 VARCHAR(130),    
@VSFE_OPER VARCHAR(10),     
@VDFE_OPER TD_DT_001    
    
-- ASIGNO LAS VARIABLES DE TRABAJO CON LAS VARIABLES DE ENTRADA    
    
SELECT @VSCO_USUA = @ISCO_USUA    
SELECT @VSCA_WH01 = @ISCA_WH01     
SELECT @VSCA_WH02 = @ISCA_WH02    
SELECT @VSCA_WHER = @VSCA_WH01 + @VSCA_WH02    
SELECT @VSFE_OPER = @ISFE_OPER    
SELECT @VDFE_OPER = Convert(Datetime, @VSFE_OPER, 103)    
    
-- INVOCA PROCEDURE PARA CARGA DE TABLAS TEMPORALES (SALDOS)    
EXECUTE SP_TWSACA_REPO_Q91 @VDFE_OPER    
    
-- INVOCA PROCEDURE PARA CARGA DE TABLAS TEMPORALES (MOVIMIENTOS)    
EXECUTE SP_TCMOVI_CAJA_Q01 @VSCO_USUA, @VDFE_OPER    
    
    
-- PRIMER SELECT     
    
    
Execute ('SELECT T1.CO_CAJA, T1.DE_CAJA, T1.CO_MONE AS COD_MONEDA, T2.IM_SALD_INIC AS SALDO_INICIAL, T3.IM_INGR AS INGRESO, T3.IM_EGRE AS EGRESO,    
   T2.IM_SALD_INIC + T3.IM_INGR - T3.IM_EGRE AS SALDO_LIBROS, T3.IM_CHEQ_CART AS CHEQUES_CARTERA,     
   T2.IM_SALD_INIC + T3.IM_INGR - T3.IM_EGRE + T3.IM_CHEQ_CART AS SALDO_DISPONIBLE    
   FROM TMCAJA T1, #TWSACA_REPO T2, #TWPOSI_CAJA_Q01_02 T3, TMEMPR T5, TMPARA_TESO T6    
   WHERE 1=1     
    AND T1.CO_EMPR = ''' + @ISCO_EMPR + '''  '+@ISCA_WH01 + @ISCA_WH02 +    
    'AND T1.CO_EMPR = T2.CO_EMPR      
    AND T1.CO_CAJA = T2.CO_CAJA     
    AND T1.CO_EMPR = T3.CO_EMPR    
    AND T1.CO_CAJA = T3.CO_CAJA    
    AND T1.CO_EMPR = T5.CO_EMPR    
    AND T1.CO_EMPR = T6.CO_EMPR    
    AND EXISTS ( SELECT CO_EMPR  FROM  TTUSER_CAJA T4    
    Where T1.CO_EMPR = T4.CO_EMPR    
    AND T1.CO_CAJA  = T4.CO_CAJA    
    AND T4.CO_USUA = ''' + @VSCO_USUA + '''  
    AND T4.ST_REPO = ''S'')    
    ORDER BY T1.CO_MONE, T1.DE_CAJA' )    
    
    
Execute (' Select  T1.CO_MONE AS COD_MONEDA,isnull(SUM(T2.IM_SALD_INIC),''0.00'') AS SALDO_INICIAL,     
 isnull(SUM(T3.IM_INGR),''0.00'') AS INGRESO,    
 isnull(SUM(T3.IM_EGRE),''0.00'') AS EGRESO,    
 isnull(SUM(T2.IM_SALD_INIC + T3.IM_INGR - T3.IM_EGRE),''0.00'') AS SALDO_LIBROS,    
 isnull(SUM(T3.IM_CHEQ_CART),''0.00'') AS CHEQUES_CARTERA,    
 isnull(SUM(T2.IM_SALD_INIC + T3.IM_INGR - T3.IM_EGRE + T3.IM_CHEQ_CART),''0.00'') AS SALDO_DISPONIBLE    
 FROM TMCAJA T1, #TWSACA_REPO T2, #TWPOSI_CAJA_Q01_02 T3, TMEMPR T5, TMPARA_TESO T6    
 WHERE 1=1    
         AND T1.CO_EMPR = ''' + @ISCO_EMPR + '''  '+@ISCA_WH01 + @ISCA_WH02 +    
        'AND T1.CO_EMPR = T2.CO_EMPR      
  AND T1.CO_CAJA = T2.CO_CAJA    
  AND T1.CO_EMPR = T3.CO_EMPR    
  AND T1.CO_CAJA = T3.CO_CAJA    
  AND T1.CO_EMPR = T5.CO_EMPR    
  AND T1.CO_EMPR = T6.CO_EMPR    
  AND EXISTS ( SELECT CO_EMPR FROM  TTUSER_CAJA T4    
  Where T1.CO_EMPR = T4.CO_EMPR    
  AND T1.CO_CAJA  = T4.CO_CAJA    
         AND T4.CO_USUA = ''' + @VSCO_USUA + '''    
  AND T4.ST_REPO = ''S'')    
  GROUP BY T1.CO_MONE    
  ORDER BY T1.CO_MONE ' )    
    
/*------------------------------ FIN ---------------------------------*/


GO
